<?php
class Card extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
		$this->load->model ( 'Muser' );
	}
	
	function index() {
		redirect ( base_url () . 'card/balance' );
	}
	
	function balance() {
		$id = $this->session->userdata ( 'uid' );
		$data ['uid'] = $id;
		$this->load->model ( 'Mabout' );
		$data ['name'] = $this->session->userdata ( 'uname' );
		$data ['cardAboutList'] = $this->Mabout->getAboutListbyClass ( 'C' );
		$data ['aboutList'] = $this->Mabout->getAboutListbyClass ( 'A' );
		
		$this->load->helper ( 'form' );
		
		$this->load->model ( 'Mcard' );
		$data ['success'] = FALSE;
		$data ['msg'] = '';
		
		$this->form_validation->set_rules ( 'cardId', '卡号', 'trim|required' );
		$this->form_validation->set_rules ( 'test', '验证码', 'trim|required|callback_checkCaptchaCB' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['img'] = $this->getCaptcha ();
			$data ['form'] = array ('id' => 'balanceForm' );
			$this->load->view ( 'card_balance', $data );
		} else {
			$str = $this->input->post ( 'cardId' );
			$strRow = explode ( "\n", $str );
			$tmp = 0;
			foreach ( $strRow as $row ) {
				if ($tmp >= 5) {
					$data ['success'] = FALSE;
					$data ['msg'] = "单次查询数量不能高于5张！";
					break;
				}
				$data ['success'] = TRUE;
				if ((strlen ( $row ) > 0) && (strlen ( $row ) <= 15)) {
					$data ['balance'] [$tmp] = $this->Mcard->getBalancebyCardId ( trim ( $row ) );
					if ($data ['balance'] [$tmp]) {
						$data ['balance'] [$tmp] ['success'] = 'T';
					} else {
						$data ['balance'] [$tmp] ['success'] = 'F';
						$data ['balance'] [$tmp] ['errId'] = trim ( $row );
					}
				
				} else {
					$data ['balance'] [$tmp] ['success'] = 'F';
					$data ['balance'] [$tmp] ['errId'] = trim ( $row );
				}
				$tmp += 1;
			}
			$data ['img'] = $this->getCaptcha ();
			$data ['form'] = array ('id' => 'balanceForm' );
			$this->load->view ( 'card_balance', $data );
		}
	
	}
	
	function details() {
		$data ['uid'] = $this->session->userdata ( 'uid' );
		$this->load->model ( 'Mabout' );
		$data ['name'] = $this->session->userdata ( 'uname' );
		$data ['cardAboutList'] = $this->Mabout->getAboutListbyClass ( 'C' );
		$data ['aboutList'] = $this->Mabout->getAboutListbyClass ( 'A' );
		$data ['cardId'] = $this->uri->segment ( 3, 0 );
		
		if ($data ['uid']) {
			$this->load->model ( 'Mcard' );
			$data ['details'] = $this->Mcard->getCardDetailsbyCardId ( $data ['cardId'] );
			$this->load->view ( 'card_details', $data );
		} else {
			$data ['msg'] = 'login';
			$data ['url'] = base_url () . 'user/login';
			$this->load->view ( 'redirect', $data );
		}
	}
	
	function application() {
		$this->load->helper ( 'form' );
		$this->load->model ( 'Mabout' );
		$data ['uid'] = $this->session->userdata ( 'uid' );
		$data ['name'] = $this->session->userdata ( 'uname' );
		$data ['cardAboutList'] = $this->Mabout->getAboutListbyClass ( 'C' );
		$data ['aboutList'] = $this->Mabout->getAboutListbyClass ( 'A' );
		
		$this->form_validation->set_rules ( 'company', '公司名称', 'trim|max_length[100]' );
		$this->form_validation->set_rules ( 'name', '联系人', 'trim|required|min_length[2]|max_length[10]' );
		$this->form_validation->set_rules ( 'address', '公司地址', 'trim|max_length[100]' );
		$this->form_validation->set_rules ( 'phone', '联系电话', 'trim|required|min_length[4]|max_length[20]|callback_checkPhoneCB' );
		$this->form_validation->set_rules ( 'denomination1', '面额', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'denomination2', '面额', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'denomination3', '面额', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'denomination4', '面额', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'number1', '张数', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'number2', '张数', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'number3', '张数', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'number4', '张数', 'trim|max_length[11]|is_natural' );
		$this->form_validation->set_rules ( 'test', '验证码', 'trim|required|callback_checkCaptchaCB' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['img'] = $this->getCaptcha ();
			$data ['form'] = array ('id' => 'appForm' );
			$this->load->view ( 'card_application', $data );
		} else {
			$this->load->model ( 'Mcard' );
			$info ['companyName'] = $this->input->post ( 'company' );
			$info ['name'] = $this->input->post ( 'name' );
			$info ['address'] = $this->input->post ( 'address' );
			$info ['phone'] = $this->input->post ( 'phone' );
			$info ['state'] = 'F';
			$info ['denomination1'] = $this->input->post ( 'denomination1' );
			$info ['denomination2'] = $this->input->post ( 'denomination2' );
			$info ['denomination3'] = $this->input->post ( 'denomination3' );
			$info ['denomination4'] = $this->input->post ( 'denomination4' );
			$info ['number1'] = $this->input->post ( 'number1' );
			$info ['number2'] = $this->input->post ( 'number2' );
			$info ['number3'] = $this->input->post ( 'number3' );
			$info ['number4'] = $this->input->post ( 'number4' );
			if ($this->Mcard->addCardApplication ( $info )) {
				$data ['msg'] = 'card_application_T';
				$data ['total'] = $info ['denomination1'] * $info ['number1'] + $info ['denomination2'] * $info ['number2'] + $info ['denomination3'] * $info ['number3'] + $info ['denomination4'] * $info ['number4'];
				$this->load->view ( 'message', $data );
			} else {
				$data ['msg'] = 'card_application_F';
				$this->load->view ( 'message', $data );
			}
		}
	}
	
	// 功能函数
	function getCaptcha() {
		$this->load->helper ( 'captcha' );
		$this->load->helper ( 'string' );
		$vals = array ('word' => random_string ( 'numeric', 5 ), 'img_path' => './captchas/', 'img_url' => base_url () . 'captchas/', 'img_width' => 60, 'img_height' => 30, 'expiration' => 120 );
		$cap = create_captcha ( $vals );
		$this->session->set_userdata ( 'verifyCode', $cap ['word'] );
		return $cap ['image'];
	}
	
	function checkCaptchaCB($str) {
		if (strcasecmp ( $str, $this->session->userdata ( 'verifyCode' ) ) == 0) {
			return TRUE;
		} else {
			$this->form_validation->set_message ( 'checkCaptchaCB', '%s不正确' );
			return FALSE;
		}
	}
	
	function checkPhoneCB($str) {
		if (preg_match ( "/^[0-9-]{8,20}$/", $str ))
			return TRUE;
		else {
			$this->form_validation->set_message ( 'checkPhoneCB', '%s不正确' );
			return FALSE;
		}
	}
}